package com.samebuilding.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.samebuilding.dao.PrivateRecMesDAO;
import com.samebuilding.domain.PrivateReceivedMessage;
import com.samebuilding.domain.User;

public class PrivateRecMesDAOImpl extends HibernateDaoSupport implements
		PrivateRecMesDAO {

	private static final String FIND_ALL_RECEIVE = "from PrivateReceivedMessage as p where p.recevierUserno = ? order by p.date desc";
	private static final String FIND_NOT_READ_SIZE = "select count(*) from PrivateReceivedMessage as p where p.recevierUserno = ? and p.hasRead = 0 ";
	private static final String FIND_ALL_RECEIVE_SIZE = "select count(*) from PrivateReceivedMessage as p where p.recevierUserno = ?";
	private static final String READ_MESSAGE = "update PrivateReceivedMessage set hasRead = 1 where recevierUserno = ? and hasRead = 0";
	private static final String REMOVE_BY_ID = "delete from PrivateReceivedMessage where id = ?";
	
	public List findAllRec(final User user,final int from,final int size) {
		ReadMessage(user);
		List message=this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q=session.createQuery("from PrivateReceivedMessage as p where p.recevierUserno=? order by p.date desc");
				q.setParameter(0, user.getUserNo());
				q.setFirstResult(from);
				q.setMaxResults(size);
				return q.list();
			}
		});
		return message;
	}

	public void remove(PrivateReceivedMessage prm) {
		if(this.findById(prm.getId())!=null)
		this.getHibernateTemplate().delete(prm);
	}

	public void save(PrivateReceivedMessage prm) {
		this.getHibernateTemplate().save(prm);
	}

	public List findNotReadRec(final User u) {
		final String hql="from PrivateReceivedMessage as p where p.hasRead=0 and p.recevierUserno=?";
		List list=this.getHibernateTemplate().executeFind(new HibernateCallback() {

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query q=session.createQuery(hql);
				q.setParameter(0, u.getUserNo());
				return q.list();
			}
			
		});
		if(list==null){
			list=new ArrayList();
		}
		return list;
	}

	public void ReadMessage(User u) {
		List list=this.findNotReadRec(u);
		for(Iterator it=list.iterator();it.hasNext();){
			PrivateReceivedMessage prm=(PrivateReceivedMessage)it.next();
			prm.setHasRead(1);
			this.getHibernateTemplate().update(prm);
		}
		
	}

	public long findRecSize(User user) {
		List list=this.getHibernateTemplate().find("select count(*) from PrivateReceivedMessage as p where p.recevierUserno=?",user.getUserNo());
		if(list!=null)
			return ((Long) list.iterator().next()).longValue();
		return 0;
	}

	public PrivateReceivedMessage findById(String id) {
		List list=this.getHibernateTemplate().find("from PrivateReceivedMessage as p where p.id=?",id);
		if(list!=null&&list.size()>0){
			return (PrivateReceivedMessage)list.get(0);
		}
		return null;
	}

	public long findnotReadSize(User u) {
		List list=this.getHibernateTemplate().find("select count(*) from PrivateReceivedMessage as p where p.hasRead=0 and p.recevierUserno=?",u.getUserNo());
		if(list!=null)
			return ((Long) list.iterator().next()).longValue();
		return 0;
	}

	@SuppressWarnings("unchecked")
	public List<PrivateReceivedMessage> findAllRec(final int userNo,final int start,final int limit) {
		return (List<PrivateReceivedMessage>)getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(FIND_ALL_RECEIVE);
				query.setInteger(0, userNo);
				query.setFirstResult(start);
				query.setMaxResults(limit);
				List<PrivateReceivedMessage> list = query.list();
				return list;
			}
		});
	}

	public int findNotReadSize(final int userNo) {
		long count = (Long)this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(FIND_NOT_READ_SIZE);
				query.setInteger(0, userNo);

				return query.uniqueResult();
			}
		});
		return (int)count;
	}

	public int findRecSize(final int userNo) {
		long count = (Long)this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(FIND_ALL_RECEIVE_SIZE);
				query.setInteger(0, userNo);

				return query.uniqueResult();
			}
		});
		return (int)count;
	}

	public void readMessage(final int userNo) {
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session s) throws HibernateException,
					SQLException {
				Query query = s.createQuery(READ_MESSAGE);
				query.setInteger(0, userNo);
				query.executeUpdate();
				return null;
			}
		});
		
	}

	public void removePrivateMesById(final int id) {
		getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session s)
					throws HibernateException, SQLException {
				Query query = s.createQuery(REMOVE_BY_ID);
				query.setInteger(0, id);
				query.executeUpdate();
				return null;
			}
		});
		
	}
	

}
